package ca.etsmtl.gti785.util;

import java.util.Comparator;
import ca.etsmtl.gti785.classes.Application;
import ca.etsmtl.gti785.classes.Server;

/**
 * This class is used to compare and sort servers between them.
 * In order : distance from the mobile, name, last accessed date.
 * @see Server
 * @author Etienne Faucher
 */
public class ServerSorter implements Comparator<Server> 
{
	@Override
	public int compare(Server server1, Server server2) 
	{
    	double latitude = Application.getInstanceOf().getGPSTracker().getLatitude();
    	double longitude = Application.getInstanceOf().getGPSTracker().getLongitude();
    	
    	double server1Distance = DistanceCalculator.DistanceBetweenPlaces(server1.getLongitude(), server1.getLatitude(), longitude, latitude);
    	double server2Distance = DistanceCalculator.DistanceBetweenPlaces(server2.getLongitude(), server2.getLatitude(), longitude, latitude);
    	
    	if((server1Distance - server2Distance) == 0)
    	{
    		if(server1.getName().compareTo(server2.getName()) == 0)
    		{
    			return server1.GetLastAccessDate().compareTo(server2.GetLastAccessDate());
    		}
    		
    		return server1.getName().compareTo(server2.getName());
    	}
    	
    	return (int) (server1Distance - server2Distance);
	}

}
